const { pool } = require("../config/db");
const logger = require("./logger");

async function saveMysql(data) {
    const conn = await pool.getConnection();
    try {
        await conn.beginTransaction();

        // 检查记录是否已存在
        const [existingRows] = await conn.query(
            "SELECT id FROM douyin_videos WHERE aweme_id = ?",
            [data.aweme_id]
        );

        if (existingRows.length > 0) {
            // 更新现有记录
            const updateSql = `
        UPDATE douyin_videos SET
          platform = ?,
          create_time = ?,
          crawl_time = ?,
          author_uid = ?,
          author_sec_uid = ?,
          author_nickname = ?,
          author_signature = ?,
          author_avatar_url = ?,
          author_following_count = ?,
          author_follower_count = ?,
          author_total_favorited = ?,
          author_unique_id = ?,
          video_desc = ?,
          video_url = ?,
          cover_url = ?,
          duration = ?,
          width = ?,
          height = ?,
          comment_count = ?,
          digg_count = ?,
          download_count = ?,
          forward_count = ?,
          play_count = ?,
          share_count = ?,
          music_id = ?,
          music_title = ?,
          music_author = ?,
          music_duration = ?,
          hashtags = ?
        WHERE aweme_id = ?
      `;

            await conn.query(updateSql, [
                data.platform,
                data.create_time,
                data.crawl_time,
                data.author_uid,
                data.author_sec_uid,
                data.author_nickname,
                data.author_signature,
                data.author_avatar_url,
                data.author_following_count,
                data.author_follower_count,
                data.author_total_favorited,
                data.author_unique_id,
                data.video_desc,
                data.video_url,
                data.cover_url,
                data.duration,
                data.width,
                data.height,
                data.comment_count,
                data.digg_count,
                data.download_count,
                data.forward_count,
                data.play_count,
                data.share_count,
                data.music_id,
                data.music_title,
                data.music_author,
                data.music_duration,
                data.hashtags,
                data.aweme_id
            ]);

            logger.info(`Updated existing record for aweme_id: ${data.aweme_id}`);
        } else {
            // 插入新记录
            const insertSql = `
        INSERT INTO douyin_videos (
          platform, aweme_id, create_time, crawl_time,
          author_uid, author_sec_uid, author_nickname, author_signature,
          author_avatar_url, author_following_count, author_follower_count,
          author_total_favorited, author_unique_id, video_desc, video_url,
          cover_url, duration, width, height, comment_count, digg_count,
          download_count, forward_count, play_count, share_count,
          music_id, music_title, music_author, music_duration, hashtags
        ) VALUES (
          ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
          ?, ?, ?, ?, ?, ?, ?, ?, ?
        )
      `;

            await conn.query(insertSql, [
                data.platform,
                data.aweme_id,
                data.create_time,
                data.crawl_time,
                data.author_uid,
                data.author_sec_uid,
                data.author_nickname,
                data.author_signature,
                data.author_avatar_url,
                data.author_following_count,
                data.author_follower_count,
                data.author_total_favorited,
                data.author_unique_id,
                data.video_desc,
                data.video_url,
                data.cover_url,
                data.duration,
                data.width,
                data.height,
                data.comment_count,
                data.digg_count,
                data.download_count,
                data.forward_count,
                data.play_count,
                data.share_count,
                data.music_id,
                data.music_title,
                data.music_author,
                data.music_duration,
                data.hashtags
            ]);

            logger.info(`Inserted new record for aweme_id: ${data.aweme_id}`);
        }

        await conn.commit();
    } catch (error) {
        await conn.rollback();
        logger.error('Error saving to MySQL:', error);
        throw error;
    } finally {
        conn.release();
    }
}

module.exports = {
    saveMysql
}; 